package com.example.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author jie
 * @date 2021/11/25 -11:23
 */
@NoArgsConstructor
@Data
public class RestfulApiResponse<T> {

    public static final String SUCCESS_CODE = "0";
    public static final String FAILURE_CODE = "-1";
    public static final int SUCCESS_STATE = 0;
    public static final int FAILURE_STATE = 1;
    public static final int ERROR_STATE = -1;
    public static final String SUCCESS_MESSAGE = "操作成功";

    private Integer returnCode;
    private String returnMsg;
    private String errorCode;
    private String stackTrace;
    private T result;

    public RestfulApiResponse(Integer returnCode, String errorCode, String returnMsg) {
        this.returnCode = returnCode;
        this.returnMsg = returnMsg;
        this.errorCode = errorCode;
    }

    public RestfulApiResponse(Integer returnCode, String errorCode, String returnMsg, T result) {
        this.returnCode = returnCode;
        this.returnMsg = returnMsg;
        this.errorCode = errorCode;
        this.result = result;
    }

    public RestfulApiResponse(Integer returnCode, String errorCode, String returnMsg, String stackTrace) {
        this.returnCode = returnCode;
        this.returnMsg = returnMsg == null ? stackTrace: stackTrace;
        this.errorCode = errorCode;
        this.stackTrace = stackTrace;
    }

    /**
     * 成功
     */
    public static <T> RestfulApiResponse<T> success() {
        return new RestfulApiResponse<>(SUCCESS_STATE, SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    public static <T> RestfulApiResponse<T> success(T data) {
        return new RestfulApiResponse<>(SUCCESS_STATE, SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static <T> RestfulApiResponse<T> success(String message) {
        return new RestfulApiResponse<>(SUCCESS_STATE, SUCCESS_CODE, message);
    }

    public static <T> RestfulApiResponse<T> success(String message, T data) {
        return new RestfulApiResponse<>(SUCCESS_STATE, SUCCESS_CODE, message, data);
    }

    /**
     * 失败
     */
    public static <T> RestfulApiResponse<T> failure(String code, String message) {
        return new RestfulApiResponse<>(FAILURE_STATE, code, message, "");
    }

    public static <T> RestfulApiResponse<T> failure(String code, String message, String stackTrace) {
        return new RestfulApiResponse<>(FAILURE_STATE, code, message, stackTrace);
    }

    /**
     * 错误
     */
    public static <T> RestfulApiResponse<T> error(String message, String stackTrace) {
        return new RestfulApiResponse<>(ERROR_STATE, "-1", message, stackTrace);
    }
}

